package it.unibo.iss.domotica.edDomain;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class CatConsumption {
	
	private CatConsumptionEnum catConsumption;
	private double minConsumption;
	private double maxConsumption;
	private static final String propertiesPath = "G:\\Documenti\\Universita\\Magistrale\\Ingegneria dei Sistemi Software M\\workspace\\domotica.scontrol\\src\\system.properties";
	
	public CatConsumption() {
		this(CatConsumptionEnum.LOW);
	}
	
	public CatConsumption(CatConsumptionEnum catConsumption) {
		boolean exceptionOccured = false;
		
		this.catConsumption = catConsumption;
		
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(propertiesPath));
		} catch (FileNotFoundException e) {
//			e.printStackTrace();
			exceptionOccured = true;
			loadDefaultValues();
		} catch (IOException e) {
//			e.printStackTrace();
			exceptionOccured = true;
			loadDefaultValues();
		}
		
		if(!exceptionOccured)
			loadValuesFromProperties(prop);
	}
	
	public CatConsumption(CatConsumptionEnum catConsumption, Properties prop) {
		this.catConsumption = catConsumption;
		
		if(prop == null) {
			loadDefaultValues();
		}
		else {
			loadValuesFromProperties(prop);
		}
	}
	
	public double getMinConsumption() {
		return minConsumption;
	}

	public double getMaxConsumption() {
		return maxConsumption;
	}

	private void loadDefaultValues() {
		if(catConsumption == CatConsumptionEnum.LOW) {
			minConsumption = 1;
			maxConsumption = 500;
		}
		else if(catConsumption == CatConsumptionEnum.MEDIUM) {
			minConsumption = 501;
			maxConsumption = 1000;
		}
		else if(catConsumption == CatConsumptionEnum.HIGH) {
			minConsumption = 1001;
			maxConsumption = 2000;
		}
	}

	private void loadValuesFromProperties(Properties prop) {
		if(catConsumption == CatConsumptionEnum.LOW) {
			minConsumption = Double.valueOf(prop.getProperty("minConsumoBasso"));
			maxConsumption = Double.valueOf(prop.getProperty("maxConsumoBasso"));
		}
		else if(catConsumption == CatConsumptionEnum.MEDIUM) {
			minConsumption = Double.valueOf(prop.getProperty("minConsumoMedio"));
			maxConsumption = Double.valueOf(prop.getProperty("maxConsumoMedio"));
		}
		else if(catConsumption == CatConsumptionEnum.HIGH) {
			minConsumption = Double.valueOf(prop.getProperty("minConsumoAlto"));
			maxConsumption = Double.valueOf(prop.getProperty("maxConsumoAlto"));
		}
		
	}

	public static CatConsumption getCatConsumption(CatConsumptionEnum cat) {
		return new CatConsumption(cat);
	}
	
	public static CatConsumption getCatConsumption(CatConsumptionEnum cat, Properties prop) {
		return new CatConsumption(cat, prop);
	}

	public CatConsumptionEnum getCatConsumptionEnum() {
		return catConsumption;
	}	
	
}
